var myScroll,
    pullDownEl, pullDownOffset,
    pullUpEl, pullUpOffset;
var lock=true;
$(function(){
	//初始化下拉和上拉箭头
	pullDownEl = document.getElementById('pullDown');
    pullDownOffset = pullDownEl.offsetHeight;
    pullUpEl = document.getElementById('pullUp');  
    pullUpOffset = pullUpEl.offsetHeight;
	
    //拖拽刷新
	myScroll = new iScroll('wrapper',{
		scrollbarClass: 'myScrollbar',
		onRefresh: function () {
			if (lock) {
			lock=false;
            if (pullDownEl.className.match('loading')) {
                pullDownEl.className = '';
                pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉刷新...';
            } else if (pullUpEl.className.match('loading')) {
                pullUpEl.className = '';
                pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
            }
            	lock=true;
		}
		return false;
        },
        onScrollMove: function () {
        	if (lock) {
			lock=false;
            if (this.y > 5 && !pullDownEl.className.match('flip')) {
                pullDownEl.className = 'flip';
                pullDownEl.querySelector('.pullDownLabel').innerHTML = '松手开始更新...';
                this.minScrollY = 0;
                $("#pullDown").show();
            } else if (this.y < 5 && pullDownEl.className.match('flip')) {
                pullDownEl.className = '';
                pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉刷新...';
                this.minScrollY = -pullDownOffset;
                $("#pullDown").show();
            } else if (this.y < (this.maxScrollY - 5) && !pullUpEl.className.match('flip')) {
                pullUpEl.className = 'flip';
                pullUpEl.querySelector('.pullUpLabel').innerHTML = '松手开始更新...';
                this.maxScrollY = this.maxScrollY;
                $("#pullUp").show();
            } else if (this.y > (this.maxScrollY + 5) && pullUpEl.className.match('flip')) {
                pullUpEl.className = '';
                pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
                this.maxScrollY = pullUpOffset;
                $("#pullUp").show();
            }
            	lock=true;
		}
		return false;
        },
        onScrollEnd: function () {
        	if (lock) {
			lock=false;
            if (pullDownEl.className.match('flip')) {
                pullDownEl.className = 'loading';
                pullDownEl.querySelector('.pullDownLabel').innerHTML = '加载中...';               
                pullDownAction();   // ajax call
            } else if (pullUpEl.className.match('flip')) {
                pullUpEl.className = 'loading';
                pullUpEl.querySelector('.pullUpLabel').innerHTML = '加载中...';               
                pullUpAction(); // ajax call
            }
            lock=true;
		}
        return false;
        }
	});
	
	//向上拖拽刷新
	function pullUpAction(){

			GoodsApp.loadData(GoodsApp.getPage());
	}
	
	//向下拖拽刷新
	function pullDownAction(){
			GoodsApp.init();
	}
	
	//初始化首页订单
	GoodsApp.init();
	
	//点击切换头部订单状态
//	$("nav.order-nav ul.clear li a").click(function(){
//		
//		$("nav.order-nav ul.clear li a").removeClass('order_select');
//		var name = $(this).attr('name');
//		GoodsApp.setPage(1);
//		GoodsApp.loadData(1,name);
//	});
});
var GoodsApp = function(){
	var page = 1;
	var pageSize = 6;
	var category;
	return{
		/**
		 * 初始化
		 */
		init:function(){
			page=1;
			GoodsApp.loadData(1);
			GoodsApp.bindEvent();
		},
		/**
		 * 设置当前页数
		 */
		setPage:function(pg){
			GoodsApp.page = pg;
		},
		/**
		 * 获取当前页面
		 */
		getPage:function(){
			
			return page;
		},
		/**
		 * 搜索按钮功能
		 */
		serach:function(){
			page=1;
			GoodsApp.loadData(page);
		},
		selectCategory:function(e,id){
			$(".tab-center").removeClass("active");
			$(e).addClass("active");
			page=1;
			category=id;
			GoodsApp.loadData(page,id);
		},
		//绑定按钮事件
		bindEvent:function(){
			 $("#serach").keydown(function(event){
			    event=document.all?window.event:event;
			    if((event.keyCode || event.which)==13){
			    	GoodsApp.serach();
			    	var url = $("#APP_ROOT_PATH").val() + 'goods/page?search=' + $("#serach").val();
			    	var stateObject = {};
			    	var title = "商品列表";
			    	history.pushState(stateObject,title,url);
			    }
			   }); 
		},
		/**
		 * 加载数据
		 */
		loadData:function(p,status){
//			var myLayer = layer.open({type: 2});

			if(status==null){
				status=category;
			}
			var url = $("#APP_ROOT_PATH").val() + 'goods/page_data';
			var params = {	name:$("#serach").val(),page:p,pageSize:pageSize,categoryId:status};
				mui.util.Ajax.asyncGET(url,params,function(res){
					
					var loaded = (p - 1) * pageSize + res.rows.length;
					if(res.rows.length <= 0){
						$(".bottom-1").text('没有更多商品啦');
					}else{
						$(".bottom-1").text('');
					}
					
					var data = {data:res.rows};
					var bt=baidu.template;
					var html=bt('t:bt',data);
					if(p == 1){
						$("#data-content").html(html);
					}else{
						$("#data-content").append(html);
					}
					myScroll.refresh();
					$("#pullDown, #pullUp").hide();
//					layer.close(myLayer);
					page++;
				},function(){
				},function(){
				});
				
			
		},
//		loadData:function(p){
//			var url = $("#APP_ROOT_PATH").val() + 'goods/page_data';
//			var params = {
//				name:$("#serach").val(),
//				page:p,
//				pageSize:pageSize
//			};
//			mui.util.Ajax.asyncGET(url,params,function(res){
//				layer.closeAll();
//				
//				console.log(res);
//				var data = {data:res.rows};
//				var bt=baidu.template;
//				var html=bt('t:bt',data);
//				$("#data-content").html(html);
//			},function(){
//				layer.closeAll();
//			},function(){
//				layer.open({type: 2});
//			});
//		},
	}
}();